<?php

/**
 * 登陆控制器
 *
 * @author dalei.chen
 */

require 'CommonController.php';

class Admin_LoginController extends Admin_CommonController
{
    /**
     * 初始化
     * 
     * @return void
     */
    public function init()
    {
        $this->_loginIsCheck = 0;
        parent::init();
        $this->view->title = $this->_configs['project']['systemName'] . " - 管理中心";
    }
   
    /**
     * 登录
     * 
     * @return void
     */
    public function indexAction()
    {
        //如果已经登录则返回主页
        if (Application_Model_UserLogin::isLogined()) {
            redirect($this->view->projectUrl(array('controller'=>'index', 'action'=>'index')));
        }

        if ($this->_request->isPost()) {
            $userName = trim($this->_request->getPost('name'));
            $password = trim($this->_request->getPost('password'));
            $code = trim($this->_request->getPost('code'));

            $userName = trim($this->_request->getPost('userName'));
            $password = trim($this->_request->getPost('password'));
            $code = trim($this->_request->getPost('code'));
            if (!isset($_SESSION["{$this->_configs['project']['cookiePrefix']}_imageCode"])) {
                $_SESSION["{$this->_configs['project']['cookiePrefix']}_imageCode"] = rand(1000, 9999);
                exit($this->view->message('验证码初始化错误，请返回重新填写。'));
            } else if ($code == '') {
                $_SESSION["{$this->_configs['project']['cookiePrefix']}_imageCode"] = rand(1000, 9999);
                exit($this->view->message('请输入验证码。'));
            } else if ((strtoupper($code) !== $_SESSION["{$this->_configs['project']['cookiePrefix']}_imageCode"])) {
                $_SESSION["{$this->_configs['project']['cookiePrefix']}"] = '';
                exit($this->view->message('验证码错误，请返回重新填写。'));
            } else if (empty($userName) || empty($password)){
                $_SESSION["{$this->_configs['project']['cookiePrefix']}_imageCode"] = rand(1000, 9999);
                exit($this->view->message('用户名和密码不能为空，请返回填写。'));
            } else {
                $userLoginModel = new Application_Model_UserLogin();
                $loginStatus = $userLoginModel->login($userName, $password);
                if ($loginStatus == 10) {
                    $_SESSION["{$this->_configs['project']['cookiePrefix']}_imageCode"] = rand(1000, 9999);
                    $frow = $userLoginModel->fetchRow("`userName` = '". addslashes($userName) ."' OR `email` = '". addslashes($userName) ."' OR `mobile` = '". addslashes($userName) ."'");
                    if(empty($frow)){
                        exit($this->view->message('用户名和密码错误，请返回填写。'));
                    }else{
                        $numCo = 5 - $frow['loginFailureNum'];
                        if($numCo == 0){
                            exit($this->view->message('您已连续5次输错密码，账号锁定24小时。如有疑问请和平台研发部联系。'));
                        } else {
                            exit($this->view->message('用户名和密码错误，您还有' . $numCo . '次输入的机会，请返回填写。'));
                        }
                    }
                } else if ($loginStatus == 20) {
                    $_SESSION["{$this->_configs['project']['cookiePrefix']}_imageCode"] = rand(1000, 9999);
                    exit($this->view->message('你的账户被锁定，如有问题请与管理员联系。'));
                } else if ($loginStatus == 40) {
                    $_SESSION["{$this->_configs['project']['cookiePrefix']}_imageCode"] = rand(1000, 9999);
                    exit($this->view->message('您连续5次输错密码，账号锁定24小时。如有疑问请和平台研发部联系。'));
                } else {
                    redirect($this->view->projectUrl(array('controller'=>'index', 'action'=>'index')));
                }
            }
        }
    }
    
    /**
     * 首次登录，更改密码
     * 
     * @return void
     */
    public function firstLoginEditPasswordAction()
    {
        $userName = trim($this->_request->get('userName'));
        $this->view->userName = $userName;
        if(empty($userName)){
            exit('<html><body><script language="javascript">alert("账户不存在，如有问题请与管理员联系。");window.open("' . $this->view->projectUrl(array('controller'=>'login', 'action'=>'logout')) . '", "_top", "");</script></body></html>');
        }
        $userLoginModel = new Application_Model_UserLogin();
        if ($this->_request->isPost()) {
            $password = $this->_request->getPost('password');
            $password2 = $this->_request->getPost('password2');
            if($password != $password2){
                exit($this->view->message('两次密码输入不一致，请返回重新填写。'));
            }
            if(strlen($password) < 8 || strlen($password) >30){
                exit($this->view->message('密码中必须含有数字，小写字母，大写字母，长度必须在8-30范围内。'));
            }
            if (!preg_match("/[A-Z]+/", $password) || !preg_match("/[a-z]+/", $password) || !preg_match("/[0-9]+/", $password)) {
                exit($this->view->message('密码中必须含有数字，小写字母，大写字母，长度必须在8-30范围内。'));
            }
            $updateArr = array();
            $updateArr['password'] = md5($password);
            $updateArr['newUserIsResetPassword'] = 1;
            $userLoginModel->update($updateArr, "`userName` = '". addslashes($userName) ."' OR `email` = '". addslashes($userName) ."' OR `mobile` = '". addslashes($userName) ."'");
            $userLoginModel->logout();
            $userLoginModel->login($userName, $password);
            redirect($this->view->projectUrl(array('controller'=>'index', 'action'=>'index')));
            exit;
        }
        $cookieModel = new Application_Model_Cookie();
        if ($cookieModel->getCookie('adminUserName') != $userName) {
            exit('<html><body><script language="javascript">alert("违法操作，如有问题请与管理员联系。");window.open("' . $this->view->projectUrl(array('controller'=>'login', 'action'=>'logout')) . '", "_top", "");</script></body></html>');
        }
    }
    
    /**
     * 退出
     * 
     * @return void
     */
    public function logoutAction()
    {
        $userLoginModel = new Application_Model_UserLogin();
        $userLoginModel->logout();
        redirect($this->view->projectUrl(array('action'=>'index')));
        exit;
    }
}